package com.tongji.housediy.view.component3D
{
	import alternativa.engine3d.core.Vertex;
	import alternativa.engine3d.materials.Material;
	import alternativa.engine3d.materials.TextureMaterial;
	import alternativa.engine3d.objects.Mesh;
	
	public class CurvedWall extends Wall
	{
		public function CurvedWall(point1x:Number, point1y:Number, point2x:Number, point2y:Number, point3x:Number, point3y:Number, height:Number, material:TextureMaterial, segment:uint = 20)
		{
			resetMaterial(material);
			this.length = Math.sqrt((point1x - point2x) * (point1x - point2x) + (point1y - point2y) * (point1y - point2y));
			this.height = height;
			
			var x2:Number = 1 * point1x - 2 * point2x + 1 * point3x;
			var x1:Number = -2 * point1x + 2 * point2x;
			var x0:Number = 1 * point1x;
			var y2:Number = 1 * point1y - 2 * point2y + 1 * point3y;
			var y1:Number = -2 * point1y + 2 * point2y;
			var y0:Number = 1 * point1y;
			
			var d1:Number;
			var x:Number;
			var y:Number;
			var curveline:Array = new Array();
			for (d1 = 0; d1 < 1 + 1 / segment; d1 += 1 / segment )
			{
				x = x2 * d1 * d1 + x1 * d1 + x0;
				y = y2 * d1 * d1 + y1 * d1 + y0;
				curveline.push(x,y);
			}
			var d2:Number;
			for (d2 = 0; d2 < segment * 2 + 2; d2 += 2)	
			{
				var v1:Vertex = this.addVertex(curveline[d2],  curveline[d2 + 1],   0, d2 / segment / 2, 0);
				var v2:Vertex = this.addVertex(curveline[d2],  curveline[d2 + 1], height / 4, d2 / segment / 2, 1 / 4);
				var v3:Vertex = this.addVertex(curveline[d2],  curveline[d2 + 1], height * 3 / 4, d2 / segment / 2, 3 / 4);
				var v4:Vertex = this.addVertex(curveline[d2],  curveline[d2 + 1], height, d2 / segment / 2, 1);
			}
			for (var i:int = 0; i<segment; i++)
			{
				this.addFace(Vector.<Vertex>([this.vertices[4 * i], this.vertices[4 * i + 4],this.vertices[4 * i + 5],this.vertices[4 * i + 1]]),material);
				this.addFace(Vector.<Vertex>([this.vertices[4 * i + 1], this.vertices[4 * i + 5],this.vertices[4 * i + 6],this.vertices[4 * i + 2]]),material);
				this.addFace(Vector.<Vertex>([this.vertices[4 * i + 2], this.vertices[4 * i + 6],this.vertices[4 * i + 7],this.vertices[4 * i + 3]]),material);
			}
			this.calculateFacesNormals();
		}
	}
}